package dao.impl;

import dao.DeptDao;
import entity.Dept;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

public class DeptDaoImpl extends BaseDao implements DeptDao {
    @Override
    public List<Dept> GetDeptList() throws Exception{
        connection=getConnection();
        String sql="select * from dept";
            query(sql,null);
        /* statement = connection.prepareStatement(sql);
         resultSet = statement.executeQuery();*/
        ArrayList<Dept> list = new ArrayList<>();
        while (resultSet.next()){
            int deptno = resultSet.getInt("deptno");
            String dname = resultSet.getString("dname");
            String loc = resultSet.getString("loc");
            Dept dept = new Dept(deptno,dname,loc);
            list.add(dept);
        }
close(resultSet,statement,connection);

        return list;
    }

    @Override
    public int UpdeteDept(Dept dept) throws Exception{
        connection=getConnection();
        String sql="update dept set loc=? where deptno=?";
         statement = connection.prepareStatement(sql);
        Object[] objects = new Object[2];
        objects[0] = dept.getLoc();
        objects[1] = dept.getDeptno();
        int update = update(sql, objects);
       /* statement.setObject(1,dept.getLoc());
        statement.setObject(2,dept.getDeptno());
        int i = statement.executeUpdate();*/
        close(connection, statement);
        // update(sql,dept);
        return update;
    }

    @Override
    public int GetSelectDept(int deptno) throws Exception{
        connection=getConnection();
        String sql="select * from dept where deptno=?";
        return 0;
    }
}